class Admin::ProductImagesController < Admin::BasesController
  before_filter :init_data
  def index
    @title = t("h2.product_images")
    @search = ProductImage.new_search(params[:search])
    @search.order_by ||= :id
    @search.order_as ||= :desc
    @product_images = @search.all.paginate(:page => params[:page] || 1, :per_page=> params[:per_page] || 50)
    @product_image = ProductImage.new
  end

  def new
    @title = t('h2.new_product_image')
    @product_image = ProductImage.new
  end

  def create
    @product_image = ProductImage.new(params[:product_image])
    @product_image.product_id = params[:product_id]
    if @product_image.save
      flash[:notice] = "创建成功！"
      redirect_to admin_product_product_images_path(@product)
    else
      @title = t("h2.product_images")
      @search = ProductImage.new_search(params[:search])
      @search.order_by ||= :id
      @search.order_as ||= :desc
      @product_images = @search.all.paginate(:page => params[:page] || 1, :per_page=> params[:per_page] || 50)
      render :action => :index
    end
  end

  def edit
    @title = t("h2.edit_product_image")
    @product_image = ProductImage.find(params[:id])
  end

  def show
    @product_image = ProductImage.find(params[:id])
    render :edit
  end

  def update
    @product_image = ProductImage.find(params[:id])
    @product_image.product_id = params[:product_id]
    if @product_image.update_attributes(params[:product_image])
      flash[:notice] = "修改成功！"
      redirect_to admin_product_product_images_path(@product)
    else
      render :action => 'edit'
    end
  end

  def destroy
    @product_image = ProductImage.find(params[:id])
    @product_image.destroy

    respond_to do |format|
      format.html { redirect_to(admin_product_product_images_url(@product)) }
      format.xml { head :ok }
    end
  end
  def init_data
    @product = Product.find(params[:product_id])
  end
end
